<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *活动管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','group_id','mannum','address','image','hdp','organize','remark','status','time','end','duration','isshow','money','ispay','paixu','createtime');
	$list=pdo_getslice('jy_adbaoming_list',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$num1=pdo_count('jy_adbaoming_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id'],'status'=>1));
			$num2=pdo_count('jy_adbaoming_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id'],'status'=>2));
			$num3=pdo_count('jy_adbaoming_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id'],'status'=>4));
			$item['applynum']=$num1+$num2+$num3;
			$item['url']=custom_url('jy_adbaoming/detail//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'活动列表'),
			array('op'=>'edit','title'=>'新增报名活动'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'活动id','type'=>'string'),
			array('name'=>'title','title'=>'活动名称','type'=>'string'),
			array('type'=>'select','title'=>'分类','name'=>'group_id','options'=>'groups'),
			array('name'=>'address','title'=>'活动地址','type'=>'string'),
			array('name'=>'mannum','title'=>'允许报名人数','type'=>'string'),
			array('name'=>'applynum','title'=>'已报名人数','type'=>'string'),
			array('name'=>'time','title'=>'活动时间','type'=>'string'),
			array('name'=>'end','title'=>'报名截止时间','type'=>'string'),
			array('name'=>'money','title'=>'所需费用','type'=>'string'),
			array('name'=>'status','title'=>'是否开启','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'活动链接','type'=>'copy'),
		),),
		'search'=>array(
			array('name'=>'title','title'=>'活动名称','type'=>'string','width'=>'15'),
		)
	);
	$return['groups']=pdo_getall('jy_adbaoming_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_adbaoming_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['diyfrom']=$item['diyfrom']?json_decode($item['diyfrom'],true):array();
	foreach($item['diyfrom'] as &$sv){
			if($sv['value']){
				$new=';';
				foreach($sv['value'] as &$iv){
					$new.=$iv['title'].';';
				}
				$sv['value']=$new;
			}
	}
	$item['hdp']=json_decode($item['hdp']);
	$item['remark']=json_decode($item['remark']);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回活动列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'报名基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'名称','name'=>'title','help'=>''),
					array('type'=>'select','title'=>'分类','name'=>'group_id','options'=>'groups'),
					array('type'=>'image','title'=>'报名封面图','name'=>'image','help'=>''),
					array('type'=>'images','title'=>'报名窗口图','name'=>'hdp','help'=>'报名详情页顶部图片显示'),
					array('type'=>'video','title'=>'报名视频','name'=>'video','help'=>'报名视频'),
					array('type'=>'images','title'=>'报名详情图','name'=>'remark','help'=>'报名详情详情图'),
					array('type'=>'string','title'=>'活动地址','name'=>'address','help'=>''),
					array('type'=>'time','title'=>'活动时间','name'=>'time','help'=>''),
					array('type'=>'time','title'=>'报名截止时间','name'=>'end','help'=>''),
					array('type'=>'string','title'=>'现场活动预计时长','name'=>'duration','help'=>'单位：小时，只需填写数字;部分现场活动有时间；不填表示没有现场活动！'),
					array('type'=>'string','title'=>'活动允许最大报名人数','name'=>'mannum','help'=>''),
					array('type'=>'switch','title'=>'是否付费','name'=>'ispay','help'=>''),
					array('name'=>'money','title'=>'所需费用','type'=>'string','show'=>array('ispay'=>1)),
					array('type'=>'string','title'=>'报名按钮文字','name'=>'btntitle','help'=>'默认《立即报名》'),
					array('type'=>'string','title'=>'活动发起方','name'=>'organize','help'=>'不填则不显示活动发起方'),
					array('name'=>'star','title'=>'评分星数','type'=>'star','options'=>'star'),
					array('type'=>'richtext','title'=>'活动介绍','name'=>'content'),
					array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
				//	array('type'=>'switch','title'=>'是否首页','name'=>'isshow','help'=>''),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>''),
				)
			),
			array(
					'title'=>'自定义表单',
						array('type'=>'tip','title'=>'默认包含：联系人，联系电话；无需重复添加这两个表单'),	
							'list'=>array(
								array('type'=>'array','name'=>'diyfrom','add_tip'=>'添加一个字段',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'radio','name'=>'type','title'=>'字段类型','options'=>'types'),	
									array('type'=>'radio','name'=>'ismust','title'=>'必填','options'=>'ismust'),	
									array('type'=>'string','name'=>'paixu','title'=>'排序'),	
									array('type'=>'string','name'=>'value','title'=>'选项信息'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'select','name'=>'type','title'=>'字段类型','options'=>'type','option_title'=>'type'),
									array('type'=>'switch','name'=>'ismust','title'=>'必填','help'=>''),
									array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'从小到大顺序排序'),
									array('type'=>'string','name'=>'value','title'=>'选项信息','show'=>array('type'=>array(4,5)),'help'=>'单选和多选设置此条信息，选项之间以英文分号;隔开，例如:选项1;选项2;选项3'),
								)
								),
							)
			),
		)
	);
	$return['type']=array(array('id'=>1,'type'=>'字符串'),array('id'=>2,'type'=>'数字'),array('id'=>3,'type'=>'图片'),array('id'=>4,'type'=>'单选'),array('id'=>5,'type'=>'多选'),array('id'=>6,'type'=>'文本'),array('id'=>7,'type'=>'地址'));
	$return['types']=array('1'=>'字符串','2'=>'数字','3'=>'图片','4'=>'单选框','5'=>'多选框','6'=>'文本','7'=>'地址');
	$return['ismust']=array('0'=>'否','1'=>'是');
	$return['groups']=pdo_getall('jy_adbaoming_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['title'])){
		exi('请完善标题','error');
	}
	if(empty($postdata['image'])){
		exi('请完善封面图','error');
	}
	if(empty($postdata['hdp'])){
		exi('请完善报名窗口图','error');
	}
	if(empty($postdata['address'])){
		//exi('请完善地址','error');
	}
	if(empty($postdata['time'])){
		//exi('请完善活动时间','error');
	}
	if(empty($postdata['end'])){
		//exi('请完善报名截止时间','error');
	}
	if(!$postdata['mannum']){
		//exi('请完善活动最大报名数','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'group_id'=>intval($postdata['group_id']),
		'btntitle'=>trim($postdata['btntitle']),
		'address'=>trim($postdata['address']),
		'image'=>trim($postdata['image']),
		'time'=>trim($postdata['time']),
		'end'=>trim($postdata['end']),
		'duration'=>trim($postdata['duration']),
		'organize'=>trim($postdata['organize']),
		'ispay'=>intval($postdata['ispay']),
		'money'=>intval($postdata['money']),
		'mannum'=>intval($postdata['mannum']),
		'hdp'=>ijson_encode($postdata['hdp']),
		'remark'=>ijson_encode($postdata['remark']),
		'paixu'=>intval($postdata['paixu']),
		'isshow'=>intval($postdata['isshow']),
		'status'=>intval($postdata['status']),
		'video'=>trim($postdata['video']),
		'star'=>trim($postdata['star']),
		'content'=>trim($postdata['content']),
	);
	foreach($postdata['diyfrom'] as &$sv){
		if(strpos($sv['value'],';') !== false){
			$sv['value']=explode(';',$sv['value']);
			$new=array();
			foreach($sv['value'] as $ik=>&$iv){
				$new[$ik]['title']=$iv;
			}
			$sv['value']=$new;
		}
	}
	$data['diyfrom']=ijson_encode($postdata['diyfrom']);
	if($data['ispay']==0){
		$data['money']=0;
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_adbaoming_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_adbaoming_list',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_adbaoming_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的活动！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_adbaoming_list',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isshow'])){
		pdo_update('jy_adbaoming_list',array('isshow'=>intval($post['isshow'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
